package com.java.pertar.springlibary;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/book")
public class LoginBook {

    @Autowired
    BookSql bookSql;

    @RequestMapping("/login")
    public boolean login(String username, String password, HttpSession session) {

        log.info("登录信息：password:{}", password);
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        if ("admin".equals(username) && "admin".equals(password)) {
            session.setAttribute("user", username);
            return true;
        }
        return false;
    }

    @RequestMapping("/addbook")
    public String addBook(BookInfo book) {

        log.info("信息:{}", book);
        if (!StringUtils.hasLength(book.getBookName()) || !StringUtils.hasLength(book.getAuthor()) || book.getCount() == null || book.getPrice() == null || !StringUtils.hasLength(book.getPublish()) || book.getStatus() == null) {
            return "参数不合法";
        }

        try {
            bookSql.insert(book);
            return "";
        }catch (Exception e) {
            return "添加图书失败";
        }


    }
}
